package com.example.vv.model.projectItem.base.presenter;

import com.example.vv.fragmentWork.entity.ProjectListEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.model.projectItem.base.contract.PIContract;
import com.example.vv.model.projectItem.base.model.PIModel;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class PIPresenter extends MvpPresenter<PIContract.View,PIContract.Model> implements PIContract.Presenter {
    @Override
    public void attach(PIContract.View view) {
        super.attach(view);
        model = new PIModel();
    }

    @Override
    public void setProjectList(String api) {
        model.GET_PROJECT_LIST(api)
                .map(new Function<BaseResponse, ProjectListEntity>() {
                    @Override
                    public ProjectListEntity apply(BaseResponse baseResponse) throws Exception {
                        return new Gson().fromJson(baseResponse.getData(),ProjectListEntity.class);
                    }
                })
                .subscribe(new Consumer<ProjectListEntity>() {
                    @Override
                    public void accept(ProjectListEntity datasBeans) throws Exception {
                        view().getProjectList(datasBeans.getDatas());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
}
